package com.wuseguang.springcloud.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.service.AttendanceService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import com.wuseguang.springcloud.vo.faceVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @描述 班牌人脸考勤
 * @创建人 zm
 * @创建时间 2020/2/22
 */
@RestController
@RequestMapping("/brands")
public class AttendanceController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AttendanceService attendanceService;

    /**
     * @描述 查询所有人员考勤状态
     * @创建人 zm
     * @创建时间 2020/2/22
     */
    @RequestMapping("/queryAttendUserStatusAll")
    public Result queryAttendUserStatusAll(@RequestParam(value = "deviceid") String deviceid,//设备id
                                           @RequestParam(value = "groupid", required = false) Integer groupid,//考勤组id
                                           @RequestParam(value = "grouptype", required = false) Integer grouptype,//考勤组类型 0学生考勤 1教室考勤
                                           @RequestParam(value = "shiftid", required = false) Integer shiftid,//班次id
                                           @RequestParam(value = "time", required = false) String time) {//dialog 时间段
        logger.info(" queryAttendUserStatusAll-->deviceid={},groupid={},grouptype={},shiftid={},time={}", deviceid, groupid, grouptype, shiftid, time);
        return attendanceService.queryAttendUserStatusAll(deviceid, groupid, grouptype, shiftid, time);
    }

    /**
     * @描述 查询对应班牌所有考勤dialog时间
     * @创建人 zm
     * @创建时间 2020/2/20
     */
    @RequestMapping("/queryAttendGroupDialogAll")
    public Result queryAttendGroupDialogAll(
            @RequestParam(value = "deviceid") String deviceid) {//设备id
        return new Result(attendanceService.queryAttendGroupDialogAll(deviceid));
    }

    /**
     * @描述 班牌人脸考勤 添加考勤
     * @创建人 zm
     * @创建时间 2020/2/22
     */
    @RequestMapping("/addAttend")
    public Result addAttend(@RequestParam(value = "deviceid") String deviceid,//设备id
                            @RequestParam(value = "userid") Integer userid,//考勤人员id'
                            @RequestParam(value = "groupid") Integer groupid,//考勤组id
                            @RequestParam(value = "grouptype") Integer grouptype,//考勤组类别
                            @RequestParam(value = "shiftid") Integer shiftid,//班次id
                            @RequestParam(value = "time") String time) {
        logger.info(" addAttend-->deviceid={},groupid={},userid={},shiftid={},time={}", deviceid, groupid, userid, shiftid, time);
        return attendanceService.addAttend(deviceid, userid, groupid, grouptype, shiftid, time);
    }

    /**
     * 查询学生人脸信息
     *
     * @param deviceid
     * @return
     */
    @RequestMapping("/findStudentFace")
    public Result findStudentFace(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("查询学生人脸信息 deviceid = {}", deviceid);
        return attendanceService.findStudentFace(deviceid);
    }

    /**
     * 告知学生人脸注册情况
     *
     * @param faceList
     * @return
     */
    @RequestMapping("/informStudentFace")
    public Result informStudentFace(@RequestParam(value = "faceList") String faceList,
                                    @RequestParam(value = "deviceid") String deviceid) {
        logger.info("告知人脸注册情况 ");

        JSONArray list = JSONObject.parseArray(faceList);

        return attendanceService.informStudentFace(list, deviceid);
    }

    /**
     * 查询学生人脸信息
     *
     * @param deviceid
     * @return
     */
    @RequestMapping("/findDeleteStudentFace")
    public Result findDeleteStudentFace(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("查询删除了的人学生人脸信息 deviceid = {}", deviceid);
        return attendanceService.findDeleteStudentFace(deviceid);
    }

    /**
     * 查询学生人脸信息
     *
     * @param deviceid
     * @return
     */
    @RequestMapping("/findDeleteStudentOtherFace")
    public Result findDeleteStudentOtherFace(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("查询删除了的人学生人脸信息其他 deviceid = {}", deviceid);
        return attendanceService.findDeleteStudentOtherFace(deviceid);
    }

    /**
     * @描述 根据学生id 查学生最新一天请假记录(校牌)
     * @创建人 zm
     * @创建时间 2020/3/27
     */
    @RequestMapping("/queryVacationById")
    public Result queryVacationById(@RequestParam(value = "id") Integer id) {
        logger.info("校牌 请假离校查询 id = {}", id);
        return new Result(attendanceService.queryVacationById(id));
    }


    /**
     * @描述 (广西)校牌，请假放行
     * @创建人 zm
     * @创建时间 2020/4/4
     */
    @RequestMapping("/release")
    public Result release(@RequestParam(value = "id") Integer id) {
        logger.info("校牌 请假离校发送推送 id = {}", id);
        return attendanceService.release(id);
    }

    /**
     * @描述 家长留言/学生(班牌和家长端小程序)
     * @创建人 zm
     * @创建时间 2020/3/27
     */
    @RequestMapping("/queryParentsLeaveMessages")
    public Result queryParentsLeaveMessages(@RequestParam(value = "id") Integer id,//学生id
                                            @RequestParam(value = "type", required = false, defaultValue = "0") Integer type) {// 0 小程序 1班牌
        logger.info("家长留言/学生 id = {}", id);
        return new Result(attendanceService.queryParentsLeaveMessages(id, type));
    }

    /**
     * @描述 添加留言(班牌和家长端小程序)
     * @创建人 zm
     * @创建时间 2020/3/27
     */
    @RequestMapping("/addParentsLeaveMessages")
    public Result addParentsLeaveMessages(@RequestParam(value = "id") Integer id,
                                          @RequestParam(value = "type") Integer type,
                                          @RequestParam(value = "content") String content) {//学生id
        logger.info("家长留言/学生 id = {}", id);
        long l = attendanceService.addParentsLeaveMessages(id, type, content);
        if (l > 0) {
            return new Result();
        }
        return new Result(CodeUtil.ADD_FAILURE);
    }

    /**
     * @描述 修改留言状态(班牌和家长端小程序)
     * @创建人 zm
     * @创建时间 2020/3/27
     */
    @RequestMapping("/updateParentsLeaveMessagesState")
    public Result updateParentsLeaveMessagesState(@RequestParam(value = "id") Integer id) {//留言id
        logger.info("修改留言状态 id = {}", id);
        long l = attendanceService.updateParentsLeaveMessagesState(id);
        if (l > 0) {
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * 查询这个学校的所有人脸信息
     *
     * @param deviceid
     * @return
     */
    @RequestMapping("/findSchoolFace")
    public Result findSchoolFace(@RequestParam(value = "deviceid") String deviceid) {
        logger.info("查询这个学校的所有人脸信息");
        return attendanceService.findSchoolFace(deviceid);
    }

    /**
     * 告知全校学生人脸注册情况
     *
     * @param faceList
     * @return
     */
    @RequestMapping("/informSchoolFace")
    public Result informSchoolFace(@RequestParam(value = "faceList") String faceList,
                                   @RequestParam(value = "deviceid") String deviceid) {
        logger.info("告知人脸注册情况 ");

        JSONArray list = JSONObject.parseArray(faceList);

        return attendanceService.informSchoolFace(list, deviceid);
    }

    /**
     * @描述 根据班牌id查活动信息
     * @创建人 zm
     * @创建时间 2020/5/12
     */
    @RequestMapping("/queryParentActivitiyByDeviceid")
    public Result queryParentActivitiyByDeviceid(@RequestParam(value = "deviceid") String deviceid) {
        return attendanceService.queryParentActivitiyByDeviceid(deviceid);
    }

    /**
     * @描述 查询活动信息签到详情
     * @创建人 zm
     * @创建时间 2020/5/12
     */
    @RequestMapping("/queryActivitiyByIdList")
    public Result queryActivitiyByIdList(@RequestParam(value = "deviceid") String deviceid, @RequestParam(value = "id") Integer id) {
        return attendanceService.queryActivitiyByIdList(id, deviceid);
    }

    /**
     * @描述 根据学生卡号查询学生id
     * @创建人 zm
     * @创建时间 2020/6/30
     */
    @RequestMapping("/queryStudentIdByCardNumber")
    public Result queryStudentIdByCardNumber(@RequestParam(value = "deviceid") String deviceid, @RequestParam(value = "cardNumber") String cardNumber) {
        logger.info("queryStudentIdByCardNumber-->deviceid={},cardNumber={}", deviceid, cardNumber);
        return attendanceService.queryStudentIdByCardNumber(deviceid, cardNumber);
    }

}
